<?php
namespace App\Traits\Third;

use Illuminate\Support\Facades\Cache;
use App\Exceptions\OrdinaryException;
use App\Models\User\Users;

/**
 * 获取第三方登录信息类
 *
 * @author zx
 * @date 2018-11-20
 * @package App\Traits\Third
 */
trait TokenThird {

    /**
     * 获取微信公众号access_token
     *
     * @param bool $isGet 是否直接获取access_token TRUE:FALSE
     * @author zx
     * @date 2018-11-20
     */
    public function getAccessToken(bool $isGet=FALSE)
    {
        $data = [];
        $appId = config("secret.wechat.appid");
        $secret = config("secret.wechat.secret");
        $func = function () use (&$data, $appId, $secret) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$secret}";
            $res = json_decode(curls($url), TRUE);
            if (isset($res['access_token']) && $res['access_token']) {
                return $res['access_token'];
            } else {
                throw new OrdinaryException("获取TOKEN失败", 500);
            }
        };
        if( ! $isGet ) {
            Cache::remember("wechat:access_token:all", 110, function()use($func){
                return $func();
            });
        } else {
            return $func();
        }
    }

    /**
     * 获取随机位数
     *
     * @author zx
     * @date 2018-11-21
     * @return int
     */
    public function getNumbers():int
    {
        $model = new Users();
        $number = $model->getLastNumber();
        return $this->getAvailableNumber($number); //获取可用账号
    }

    /**
     * 验证是否可用并返回可用
     *
     * @param int $number
     * @author zx
     * @date 2018-11-21
     * @return int
     */
    private function getAvailableNumber(int $number):int
    {
        $v = [22222222, 20000000, 33333333, 30000000, 44444444, 40000000, 55555555, 50000000, 66666666, 60000000, 77777777, 70000000, 88888888, 80000000, 99999999, 90000000, 98765432, 87654321, 23456789, 76543210];
        if(in_array($number, $v)){
            $this->getAvailableNumber($number+1);
        }
        return $number;
    }


    /**
     * 重置数组下标
     *
     * @author zx
     * @param array $array
     * @param string $state source
     * @date 2018-11-23
     * @return array
     */
    public function resetArrayKeys(array $array=[], string $state):array
    {
        $res = [];
        if($array){
            switch ($state) {
                case 'gzh':
                    $res['nickname'] = $array['nickname']??'';
                    $res['sex'] = isset($array['sex'])?(in_array($array['sex'], [0,1])?$array['sex']:0):0;
                    $res['prov'] = $array['province']??'';
                    $res['city'] = $array['city']??'';
                    $res['year'] = '';
                    $res['image'] = $array['headimgurl']??"";
                    break;
                case 'xcx':
                    return $array;
                    break;
                case "qq":
                    $res['nickname'] = $array['nickname']??'';
                    $res['sex'] = 3;
                    if(isset($array['gender'])){
                        $sexies = config("user.userinfo.sex");
                        foreach($sexies as $key => $val){
                            if($val == $array['gender']) $res['sex'] = $key;
                        }
                    }
                    $res['prov'] = $array['province']??'';
                    $res['city'] = $array['city']??'';
                    $res['year'] = $array['year']??'';
                    $res['image'] = $array['figureurl_qq_1']??'';
                    break;
                case 'weibo':
                    $res['nickname'] = $array['nickname']??'';
                    $res['sex'] = isset($array['sex'])?(in_array($array['sex'], [0,1])?$array['sex']:0):0;
                    $res['prov'] = $array['province']??"";
                    $res['city'] = $array['city']??"";
                    $res['year'] = '';
                    $res['image'] = $array['headimgurl']??'';
                    break;
            }
        }
        return $res;
    }
}